package com.kevin.service.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kevin.common.exception.BusinessException;
import com.kevin.service.entity.SysUser;
import com.kevin.service.mapper.SysUserMapper;
import com.kevin.service.service.SysUserRoleService;
import com.kevin.service.service.SysUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 用户管理(SysUser)表服务实现类
 *
 * @author makejava
 * @since 2023-03-27 14:57:21
 */
@Service
@RequiredArgsConstructor
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {
    private final SysUserRoleService sysUserRoleService;
    private final PasswordEncoder passwordEncoder;

    SysUser getByUsername(String username) {
        return this.getOne(new QueryWrapper<SysUser>().eq("username", username));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveP(SysUser entity) {
        entity.setSuperAdmin(false);

        // 判断用户名是否存在
        SysUser user = getByUsername(entity.getUsername());
        if (user != null) {
            throw new BusinessException("用户名已经存在");
        }

        // 判断手机号是否存在
        user = getByMobile(entity.getMobile());
        if (user != null) {
            throw new BusinessException("手机号已经存在");
        }

        // 保存用户
        baseMapper.insert(entity);

        // 保存用户角色关系
        sysUserRoleService.saveOrUpdate(entity.getId(), entity.getRoleIdList());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(SysUser entity) {

        // 判断用户名是否存在
        SysUser user = getByUsername(entity.getUsername());
        if (user != null && !user.getId().equals(entity.getId())) {
            throw new BusinessException("用户名已经存在");
        }

        // 判断手机号是否存在
        user = getByMobile(entity.getMobile());
        if (user != null && !user.getId().equals(entity.getId())) {
            throw new BusinessException("手机号已经存在");
        }

        // 更新用户
        updateById(entity);

        // 更新用户角色关系
        sysUserRoleService.saveOrUpdate(entity.getId(), entity.getRoleIdList());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(List<Long> idList) {
        // 删除用户
        removeByIds(idList);

        // 删除用户角色关系
        sysUserRoleService.deleteByUserIdList(idList);
    }

    @Override
    public SysUser getByMobile(String mobile) {
        if (StrUtil.isEmpty(mobile)) {
            return null;
        }
        return this.getOne(new QueryWrapper<SysUser>().eq("mobile", mobile));
    }

    @Override
    public void updatePassword(Long id, String newPassword) {
        // 修改密码
        SysUser user = getById(id);
        user.setPassword(newPassword);

        updateById(user);
    }
}

